import 'dart:convert';
import 'dart:developer';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/common/my_constant.dart';
import 'package:hxdj/common/utils/my_device.dart';
import 'package:hxdj/home/locationManager/location_manager.dart';
import 'package:package_info_plus/package_info_plus.dart';

class MyTokenInterceptor extends Interceptor {
  @override
  onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();

    Map<String, dynamic> headerMap = {
      "os-type": MyDevice.isIOS ? "ios" : "android",
      "version": packageInfo.version,
      "build": packageInfo.buildNumber,
      "packageid": packageInfo.packageName,
      'app-type': "huanxindj",
      // "os-model": "apple",
      // "os-system": "apple",
      "timestamp": DateTime.now().microsecondsSinceEpoch,
    };


    if (LocationManager.result!.isNotEmpty) {
      headerMap["lon"] = LocationManager.result!["longitude"];
      headerMap["lat"] = LocationManager.result!["latitude"];
      headerMap["city"] = Uri.encodeComponent(LocationManager.result!["city"] as String);
    }

    String? token = CommonPlugin.sharedPreferences!.getString(MyConstant.token) ?? "";
    debugPrint(token);
    if (token.isNotEmpty) {
      headerMap["token"] = CommonPlugin.sharedPreferences!.getString(MyConstant.token);
    }

    options.headers.addAll(headerMap);

    DateTime startDateTime = DateTime.now();
    log("请求url--> + ${options.baseUrl.toString() + options.path.toString()}\n 请求header-> ${json.encode(options.headers).toString()}\n 请求参数-> + ${json.encode(options.data).toString()} \n 开始时间 == ${startDateTime.second}秒${startDateTime.millisecond}");
    handler.next(options);
  }

  @override
  onResponse(Response response, ResponseInterceptorHandler handler) async {
    Map<String, dynamic> jsonMap = jsonDecode(response.data ?? {});
    DateTime endDateTime = DateTime.now();

    var statusCode = jsonMap["code"];

    if (statusCode == "success" || statusCode == 0 || statusCode == "0" || statusCode == "200") {
      Map resultMap = jsonDecode(response.data);
      log("返回的url是${response.requestOptions.path.toString()}\n结束时间->${endDateTime.second}秒${endDateTime.millisecond}\n${json.encode(resultMap).toString()}");
    } else {
      log("这里接口报错了返回的url是${response.requestOptions.path.toString()}\n结束时间->${endDateTime.second}秒${endDateTime.millisecond}\n返回参数->${jsonDecode((response.data))}");
    }
    return handler.next(response);
  }

  // @override
  // onError(DioException err, ErrorInterceptorHandler handler) async {
    // DateTime endDateTime = DateTime.now();

  // try {
  //   // log("这里接口报错了返回的url是${err.requestOptions.path.toString()}\n 结束时间->${endDateTime.second}秒${endDateTime.millisecond}))}");
  //   super.onError(err, handler);
  // } catch (e) {
  //   // log("这里接口报错了返回的url是${err.requestOptions.path.toString()}\n ");
  // }

  //   return handler.next(err);
  // }
}
